var app = getApp();
Page({

  /** 页面的初始数据 **/
  data: {
    noteMaxLen: 200, //详细地址的字数限制 
    currentNoteLen: 0, //输入的字数 
    fillFilePaths: "",
    bookName:"",
    bookAuthor:"",
    evaluateContent:"",
    recommendURL: app.globalData.bookUrl + '/Recommend/SaveRecommend.do',
    updateRecommendURL: app.globalData.bookUrl + '/Recommend/updateRecommend.do',
  },

  /** 生命周期函数--监听页面加载 **/
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openId: res.data
        });
      }
    })
    var recommendToo=options.recommendToo;
    that.setData({
      recommendToo: recommendToo
    });
    if (recommendToo == "1") {
      that.setData({
        nickName: options.nickName,
        avatarUrl: options.avatarUrl
      });
    }

    if(recommendToo=="2"){
      that.setData({
        
        bookName: options.bookName,
        bookAuthor: options.bookAuthor,
        fillFilePaths:options.recommendImg,
        nickName: options.nickName,
        avatarUrl: options.avatarUrl
      });
    }
    if (recommendToo == "3") {
      that.setData({
        recommendImg: options.recommendImg,
        bookName: options.bookName,
        bookAuthor: options.bookAuthor,
        recommendId: options.recommendId,
        evaluateContent: options.recommendReason,
        isfristRecommend: options.isfristRecommend,
      });
    }
  },
 
  // in_feel: function (e) {
  //   var that = this;
  //   wx.chooseImage({
  //     count: 1,
  //     sizeType: ['original', 'compressed'],
  //     sourceType: ['album', 'camera'],
  //     success(photo) {
  //       wx.getImageInfo({
  //         src: photo.tempFiles[0].path,
  //         success: function (res) {
  //           var ctx = wx.createCanvasContext('photo_canvas');
  //           var ratio = 3;
  //           var canvasWidth = res.width
  //           var canvasHeight = res.height;

  //           that.setData({
  //             aaa: photo.tempFiles[0].path,
  //             canvasWidth2: res.width,
  //             canvasHeight2: res.height
  //           })
  //           // 保证宽高均在200以内
  //           while (canvasWidth > 200 || canvasHeight > 200) {
  //             //比例取整
  //             canvasWidth = Math.trunc(res.width / ratio)
  //             canvasHeight = Math.trunc(res.height / ratio)
  //             ratio++;
  //           }
  //           that.setData({
  //             canvasWidth: canvasWidth,
  //             canvasHeight: canvasHeight
  //           })//设置canvas尺寸
  //           ctx.drawImage(photo.tempFiles[0].path, 0, 0, canvasWidth, canvasHeight)  //将图片填充在canvas上
  //           ctx.draw()

  //           //下载canvas图片
  //           setTimeout(function () {
  //             wx.canvasToTempFilePath({
  //               canvasId: 'photo_canvas',
  //               quality: 1,
  //               width: that.data.canvasWidth,
  //               height: that.data.canvasHeight,
  //               success: function (resn) {
  //                 console.log(resn.tempFilePath)
  //                 that.setData({
  //                   fillFilePaths: resn.tempFilePath
  //                 })
  //               },
  //               fail: function (error) {
  //                 console.log(error)
  //               }
  //             })
  //           }, 100)
  //         },
  //         fail: function (error) {
  //           console.log(error)
  //         }
  //       })

  //     },
  //     fail: function (error) {
  //       // fail
  //       wx.showToast({
  //         title: '未选取',
  //         duration: 1000,
  //         image: '../../../images/icon/fail.png',
  //       })
  //     }
  //   })
  // },

  in_feel: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(photo) {
        wx.compressImage({
          src: photo.tempFiles[0].path,
          quality: 30,
          success: function (res) {
            that.setData({
              fillFilePaths: res.tempFilePath
            })
          },
          fail: function (error) {
            console.log(error)
          }
        })

      },
      fail: function (error) {
        // fail
        wx.showToast({
          title: '未选取',
          duration: 1000,
          image: '../../../images/icon/fail.png',
        })
      }
    })
  },

  //获取用户输入的用户名
  bookName: function (e) {
    this.setData({
      bookName: e.detail.value
    })
  },
  author: function (e) {
    this.setData({
      bookAuthor: e.detail.value
    })
  },
  // 字数限制
  input(event) {
    let that = this;
    var evaluateContent = event.detail.value,

      len = parseInt(evaluateContent.length);
    that.setData({
      evaluateContent: evaluateContent
    })
    this.setData({ currentNoteLen: len });
  },
  //确定
  subRecommend: function (e) {
    var that = this;

    if (that.data.fillFilePaths != "" && that.data.bookName != "" && that.data.bookAuthor != "" ) {
      wx.uploadFile({
        url: that.data.recommendURL,// 仅为示例，非真实的接口地址
        filePath: that.data.fillFilePaths,
        name: 'file',
        formData: {
          bookName: that.data.bookName,
          bookAuthor: that.data.bookAuthor,
          openid: that.data.openId,
          recommendReason: that.data.evaluateContent,
          nickName: that.data.nickName,
          avatarUrl: that.data.avatarUrl
        },
        success(res) {
          var data = res.data;
          const strToObj = JSON.parse(data);
          var code = strToObj.code;
          var msg = strToObj.msg;
          if (code == 0) {
            wx.showToast({
              title: msg,
              duration: 1000,
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: msg,
              duration: 1750,
              image: '../../../images/icon/fail.png',
            })
          }
          // do something
          setTimeout(function (e) {
          wx.switchTab({
            url: '../recommend',
            success: function (e) {
              var page = getCurrentPages();
              var prevPage = page[page.length - 2];
              prevPage.onLoad();
            }
          })
          }, 1000)
        },
        fail: function (error) {
          // fail
          wx.showToast({
            title: '推荐失败',
            duration: 1000,
            image: '../../../images/icon/fail.png',
          })
        }
      })
   
      this.setData({
        bookName: "",
        bookAuthor: "",
        fillFilePaths: "",
        evaluateContent:"",
      })
    } else {
      wx.showToast({
        title: '请填写完整',
        duration: 1000,
        image: '../../../images/icon/fail.png',
      })
    }
    // var currentStatu = e.currentTarget.dataset.statu;
    // this.util(currentStatu);
    //     bookName: options.bookName,
    // bookAuthor: options.bookAuthor,
    //   fillFilePaths: options.recommendImg,

  },
  subRecommendToo: function (e) {
    var that = this;
      wx.request({
        url: that.data.recommendURL,
        data: {
          bookAuthor: that.data.bookAuthor,
          bookName: that.data.bookName,
          openid: that.data.openId,
          recommendBookimg: that.data.fillFilePaths,
          recommendReason: that.data.evaluateContent,
          nickName: that.data.nickName,
          avatarUrl: that.data.avatarUrl
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          var code = res.data.code;
          var msg = res.data.msg;
          if (code == 0) {
            wx.showToast({
              title: msg,
              duration: 1000,
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: msg,
              duration: 1750,
              image: '../../../images/icon/fail.png',
            })
          }
          // do something
          setTimeout(function (e) {
            wx.switchTab({
              url: '../recommend',
              success: function (e) {
                var page = getCurrentPages();
                var prevPage = page[page.length - 2];
                prevPage.onLoad();
              }
            })
          }, 1000)
         
        },
        fail: function (error) {
          // fail
          wx.showToast({
            title: '评价失败',
            duration: 1000,
            image: '../../../images/icon/fail.png',
          })
        }
      })
    },
  subRecommendThree: function (e) {
    var that = this;
    if (that.data.isfristRecommend==1){
      if (that.data.fillFilePaths != ""){
      var filePath = that.data.fillFilePaths;

          wx.uploadFile({
            url: that.data.updateRecommendURL,// 仅为示例，非真实的接口地址
            filePath: filePath,
            name: 'file',
            formData: {
              recommendId: that.data.recommendId,
              recommendReason: that.data.evaluateContent
            },
            success(res) {
              var data = res.data;
              const strToObj = JSON.parse(data);
              var code = strToObj.code;
              var msg = strToObj.msg;
              if (code == 0) {
                wx.showToast({
                  title: msg,
                  duration: 1000,
                  icon: 'success'
                })
              } else {
                wx.showToast({
                  title: msg,
                  duration: 1750,
                  image: '../../../images/icon/fail.png',
                })
              }
              // do something
              setTimeout(function (e) {
              wx.switchTab({
                url: '../../personal/personal',
              })
              }, 1000)
            },
            fail: function (error) {
              // fail
              wx.showToast({
                title: '修改失败',
                duration: 1000,
                image: '../../../images/icon/fail.png',
              })
            }
          })
      }else{
        wx.request({
          url: that.data.updateRecommendURL,
          data: {
            recommendId: that.data.recommendId,
            recommendReason: that.data.evaluateContent
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success(res) {
            var data = res.data;
            var code = data.code;
            var msg = data.msg;
            if (code == 0) {
              wx.showToast({
                title: msg,
                duration: 1000,
                icon: 'success'
              })
            } else {
              wx.showToast({
                title: msg,
                duration: 1750,
                image: '../../../images/icon/fail.png',
              })
            }
            // do something
            setTimeout(function (e) {
            wx.switchTab({
              url: '../../personal/personal',
            })
            }, 1000)
          },
          fail: function (error) {
            // fail
            wx.showToast({
              title: '修改失败',
              duration: 1000,
              image: '../../../images/icon/fail.png',
            })
          }
        })
      }
     
    }else{
      wx.request({
        url: that.data.updateRecommendURL,
        data: {
          recommendId: that.data.recommendId,
          recommendReason: that.data.evaluateContent
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          var data = res.data;
          var code = data.code;
          var msg = data.msg;
          if (code == 0) {
            wx.showToast({
              title: msg,
              duration: 1000,
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: msg,
              duration: 1750,
              image: '../../../images/icon/fail.png',
            })
          }
          // do something
          setTimeout(function (e) {
          wx.switchTab({
            url: '../../personal/personal',
          })
        }, 1000)
        },
        fail: function (error) {
          // fail
          wx.showToast({
            title: '修改失败',
            duration: 1000,
            image: '../../../images/icon/fail.png',
          })
        }
      })

    }
    this.setData({
      bookName: "",
      bookAuthor: "",
      fillFilePaths: "",
      evaluateContent: "",
    })
    // var currentStatu = e.currentTarget.dataset.statu;
    // this.util(currentStatu);
    //     bookName: options.bookName,
    // bookAuthor: options.bookAuthor,
    //   fillFilePaths: options.recommendImg,

  },
})